<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="3">
            <div>照片</div>
            <a-form-model-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="perPhoto">
              <j-image-upload text="上传" v-model="model.perPhoto" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="21">
            <a-row>
              <a-col :span="24">
                <a-form-model-item label="所属分组"  :labelCol="labelCol" :wrapperCol="wrapperCol" prop="psnTypeId">
                  <j-tree-select
                    disabled
                    ref="treeSelect"
                    placeholder="请选择分组"
                    v-model="model.psnTypeId"
                    dict="wo_person_type,type_name,id"
                    pidField="pid"
                    pidValue="0"
                    hasChildField="has_child"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="perName">
                  <a-input v-model="model.perName" placeholder="请输入姓名"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="perIdcard">
                  <a-input v-model="model.perIdcard" placeholder="请输入身份证号"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="抗疫情编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="perNucleiccode">
                  <a-input v-model="model.perNucleiccode" placeholder="请输入抗疫情编码"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="perTel">
                  <a-input v-model="model.perTel" placeholder="请输入电话"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="perSex">
                  <j-dict-select-tag v-model="model.perSex"  placeholder="请输入性别" type="radio"
                                      dictCode="sex"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="出生日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="perDatebirth">
                  <j-date placeholder="请选择出生日期" v-model="model.perDatebirth"  style="width: 100%"  />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="年龄" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="perAge">
                  <a-input v-model="model.perAge" placeholder="请输入年龄"  ></a-input>
                </a-form-model-item>
              </a-col>

              <a-col :span="24">
                <a-form-model-item label="居住地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="perAddress">
                  <a-input v-model="model.perAddress" placeholder="请输入居住地址"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="国籍" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="perSex">
                  <j-dict-select-tag v-model="model.nationality"  placeholder="请选择国籍"
                                     dictCode="psn_nationality"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remarks">
                  <a-input v-model="model.remarks" placeholder="请输入备注"  ></a-input>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'WoPeopleForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          psnTypeId: [
            { required: true, message: '请选择分组!'},
          ],
          perName: [
            { required: true, message: '请输入人员姓名!'},
          ],
          perIdcard: [
            { required: true, message: '请输入人员身份证号码!'},
          ],
        },
        url: {
          add: "/people/woPeople/add",
          edit: "/people/woPeople/edit",
          queryById: "/people/woPeople/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add (typeId) {
        this.edit(this.modelDefault);
        this.model.psnTypeId=typeId;
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>